/*
Arm Tracker and Glove Demo by Noah Zerkin (noazark AT gmail.com) 2009
Released under GNU GPLv3.0 http://www.opensource.org/licenses/gpl-3.0.html
*/

void drawHandR(int s){
  pushMatrix();
  translate(a2hHx[s], a2hHy[s], 0);
  rotateZ(realGlovePitch);
  //print(glovePitch[s]);   print(" - "); print(gloveZTilt[s]);   print(" - "); println(gloveRoll[s]);
    rotateY(0.1*radians(float(wrist[0][1])));
//  wristTwist = 
///*
  if(gloveZTilt[s] >= HALF_PI){
    realGloveRoll = gloveRoll[s];
  }
  else{
    realGloveRoll = -gloveRoll[s]+PI;
  }
 // */
  rotateX(realGloveRoll);
//  rotateY(0.1*radians(float(wrist[0][0])));
//  wristTwist = 
  strokeWeight(1);
  stroke(255);
  pushMatrix();
  translate(5,0,0);

  pushMatrix();
  translate(0,0,0);
  rotateZ(-radians(fingerMiddle[s][0]));
  translate(4, 0, 0);
  pushMatrix();
  pushMatrix();
  scale(4, 1, 1);
  box(2);
  popMatrix();
  translate(4, 0, 0);
  rotateZ(-radians(fingerMiddle[s][1]));
  translate(4, 0, 0);
  pushMatrix();
  scale(4, 1, 1);
  box(2);
  popMatrix();
  //obj[s][2] = modelX(3.5,0,0);
  //obj[s][3] = modelY(3.5,0,0);
  //obj[s][4] = modelZ(3.5,0,0);
  popMatrix();
  popMatrix();

  stroke(255);
  pushMatrix();
  translate(0,0,2.5);
  rotateZ(-radians(fingerIndex[s][0]));
  translate(3.5, 0, 0);
  pushMatrix();
  pushMatrix();
  scale(3.5, 1, 1);
  box(2);
  popMatrix();
  translate(3.5, 0, 0);
  rotateZ(-radians(fingerIndex[s][1]));
  translate(3.5, 0, 0);
  pushMatrix();
  scale(3.5, 1, 1);
  box(2);
  popMatrix();
  obj[s][2] = modelX(3.5,0,0);
  obj[s][3] = modelY(3.5,0,0);
  obj[s][4] = modelZ(3.5,0,0);
/*
  pushMatrix();
  text(int(obj[s][2])+" "+int(obj[s][3])+" "+int(obj[s][4]),0,0);
  popMatrix();
  */
  popMatrix();
  popMatrix();

  stroke(255);

  pushMatrix();
  translate(0,0,-2.5);
  rotateZ(-radians(fingerRing[s][0]));
  translate(3.5, 0, 0);
  pushMatrix();
  pushMatrix();
  scale(3.5, 1, 1);
  box(2);
  popMatrix();
  translate(3.5, 0, 0);
  rotateZ(-radians(fingerRing[s][1]));
  translate(3.5, 0, 0);
  pushMatrix();
  scale(3.5, 1, 1);
  box(2);
  popMatrix();
  //obj[s][2] = modelX(3.5,0,0);
  //obj[s][3] = modelY(3.5,0,0);
  //obj[s][4] = modelZ(3.5,0,0);
  popMatrix();
  popMatrix();

  //  print(thumb[s][0]); print(" - "); print(thumb[s][1]); print(" - "); println(thumb[s][2]);
  //thumb

  pushMatrix();
  translate(-2,-1,3);
  rotateX(radians(thumb[s][0]));
  translate(0,0,1);
  rotateY(2*(-radians(thumb[s][1])));
  translate(2.75, 0, 0);
  pushMatrix();
  pushMatrix();
  scale(2.75, .75, 1);
  box(2);
  popMatrix();
  translate(2.75, 0, 0);
  rotateZ(-HALF_PI+2*(radians(thumb[s][2])));
  translate(2.75, 0, 0);
  pushMatrix();
  scale(2.75, .75, .75);
  box(2);
  popMatrix();
  //obj[s][2] = modelX(3.5,0,0);
  //obj[s][3] = modelY(3.5,0,0);
  //obj[s][4] = modelZ(3.5,0,0);
  popMatrix();
  popMatrix();

  pushMatrix();
  translate(0,0,-5);
  rotateZ(-radians(fingerPinky[s][0]));
  translate(2.75, 0, 0);
  pushMatrix();
  pushMatrix();
  scale(2.75, .75, .75);
  box(2);
  popMatrix();
  translate(2.75, 0, 0);
  rotateZ(-radians(fingerPinky[s][1]));
  translate(2.75, 0, 0);
  pushMatrix();
  scale(2.75, .75, .75);
  box(2);
  popMatrix();
  //obj[s][2] = modelX(3.5,0,0);
  //obj[s][3] = modelY(3.5,0,0);
  //obj[s][4] = modelZ(3.5,0,0);
  popMatrix();
  popMatrix();

  strokeWeight(2);
  //  fingerTipX = modelX(13.5,0,3.5);
  //  fingerTipY = modelY(13.5,0,3.5);
  //  fingerTipZ = modelZ(13.5,0,3.5);

 holdSpace = sqrt(.5*sq(grabbedObjRadius));
 grabbedObjectX[0] = modelX(-2.5,-5-holdSpace,3.5);//13.5+holdSpace,-holdSpace,3.5);
 grabbedObjectY[0] = modelY(-2.5,-5-holdSpace,3.5);//13.5+holdSpace,-holdSpace,3.5);
 grabbedObjectZ[0] = modelZ(-2.5,-5-holdSpace,3.5);//13.5+holdSpace,-holdSpace,3.5);
 palmX = modelX(-2.5,-5,3.5);//13.5+holdSpace,-holdSpace,3.5);
 palmY = modelY(-2.5,-5,3.5);//13.5+holdSpace,-holdSpace,3.5);
 palmZ = modelZ(-2.5,-5,3.5);//13.5+holdSpace,-holdSpace,3.5);
 if(pistol.holding()){
   translate(-2.5,-5,3.5);
   rotateX(-HALF_PI);
   pistol.display();
 }
 popMatrix();
 translate(2.5, 0, -1);
 scale(5,2,8);
 box(1);
 popMatrix();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

void drawHandL(int s){
  pushMatrix();
  translate(a2hHx[s], a2hHy[s], 0);
  rotateZ(2*glovePitch[s]);
  if(gloveZTilt[s] >= HALF_PI){
    rotateX(gloveRoll[s]*2);
  }
  else{
    rotateX((-2*gloveRoll[s])+PI);
  }
  strokeWeight(3);
  stroke(255);

  pushMatrix();
  rotateZ(1.5*(-radians(fingerMiddle[s][0])));
  line(0,0,0,7.5,0,0);
  translate(7.5,0,0);
  rotateZ(1.5*(-radians(fingerMiddle[s][1])));
  line(0,0,0,7.5,0,0);
  popMatrix();
  stroke(190);
  strokeWeight(3);
  pushMatrix();
  rotateZ(1.5*(-radians(fingerIndex[s][0])));
  line(0,0,-2,7.5,0,-2.5);
  translate(7.5,0,-2.5);
  rotateZ(1.5*(-radians(fingerIndex[s][1])));
  line(0,0,0,6,-2.5,-1);
  obj[s][2] = modelX(6,-2.5,1);
  obj[s][3] = modelY(6,-2.5,1);
  obj[s][4] = modelZ(6,-2.5,1);
  popMatrix();

  strokeWeight(3);
  stroke(255);

  pushMatrix();
  rotateZ(1.5*(-radians(fingerRing[s][0])));
  line(0,0,2,7.5,0,2.5);
  translate(7.5,0,2.5);
  rotateZ(1.5*(-radians(fingerRing[s][1])));
  line(0,0,0,6,0,1);
  popMatrix();

  line(0,-1,-2.5,3.75,-5,-5);
  line(3.75,-5,-5,7.5,-4,-4);

  pushMatrix();
  rotateZ(1.5*(-radians(fingerPinky[s][0])));  
  line(0,0,2.5,5,0,5);
  translate(5,0,5);
  rotateZ(1.5*(-radians(fingerPinky[s][1])));
  line(0,0,0,5,0,1);
  popMatrix();

  strokeWeight(2);
  //  fingerTipX = modelX(13.5,0,3.5);
  //  fingerTipY = modelY(13.5,0,3.5);
  //  fingerTipZ = modelZ(13.5,0,3.5);

  holdSpace = sqrt(.5*sq(grabbedObjRadius));
  grabbedObjectX[1] = modelX(13.5+holdSpace,-holdSpace,-3.5);
  grabbedObjectY[1] = modelY(13.5+holdSpace,-holdSpace,-3.5);
  grabbedObjectZ[1] = modelZ(13.5+holdSpace,-holdSpace,-3.5);  
  scale(2,0.3,.75);
  sphere(2.5);
  popMatrix();
}
